package com.fireXXX.job.analysis.crawler.common.company;

import com.fireXXX.job.analysis.crawler.boss.*;
import com.fireXXX.job.analysis.crawler.boss.constant.*;
import com.fireXXX.job.analysis.crawler.common.Page;
import com.fireXXX.job.analysis.crawler.error.CrawlerException;
import com.fireXXX.job.analysis.crawler.error.WebGatherCrawlerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CompanyCrawler {

    @Autowired
    private WebGather gather;

    @Autowired
    private BossCompanyParser bossParser;

    @Autowired
    private CompanyParser parser;

    @Autowired
    private CompanyPersister persister;

    private int retry = 3;
    private int sleepTime = 10000;

    public void crawl(CityTypeEnum city, IndustryTypeEnum industry, PublicStepTypeEnum publicStep, EmployeeNumberTypeEnum employeeNumber, Page page) {
        String content = null;
        for (int i = 0; i < retry; i++) {
            try {
                content = gather.gatherCompany(city, industry, publicStep, employeeNumber, page);
            } catch (WebGatherCrawlerException exception) {
                log.info("遭遇反爬！retry");

                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                continue;
            }

            if (content != null && !content.isEmpty()) {
                break;
            }
        }

        if (content == null || content.isEmpty()) {
            throw new CrawlerException();
        }

        persister.persist(
                city,
                parser.parse(
                        bossParser.parse(city, industry, publicStep, employeeNumber, page, content)
                )
        );
    }

}
